<template>
<!-- 添加/修改最近更新内容 -->
  <div style="text-align: left">
    <el-button class="add-button" type="primary" @click="dialogFormVisible = true">添加</el-button>
    <el-dialog
      title="添加/修改最近更新内容"
      :visible.sync="dialogFormVisible"
      @close="clear">
      <el-form :model="form" :rules="rules" style="text-align: left" ref="dataForm">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input :disabled="true" v-model="form.username" autocomplete="off" ></el-input>
        </el-form-item>
         <el-form-item label="最近更新内容" :label-width="formLabelWidth" prop="newContent">
          <el-input v-model="form.newContent" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="日期" :label-width="formLabelWidth" >
          <div class="block">
            <el-date-picker
              v-model="form.createTime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'newsAdd',
    data () {
      return {
        // 时间组件
       pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        },
        // 时间组件结束
        dialogFormVisible: false,
        rules: {
          newContent: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
        },
        form: {
          id: '',
          username: '',
          newContent: '',
          createTime: ''
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      clear () {
        this.form = {
           id: '',
          username: '',
          newContent: '',
          createTime: ''
        }
      },
      onSubmit () {
        this.$axios
          .post('/admin/news', {
            id: this.form.id,
            newContent: this.form.newContent,
            username: this.form.username,
            createTime: this.form.createTime
          }).then(resp => {
            if (resp && resp.data.code === 200) {
              this.dialogFormVisible = false
              this.$emit('onSubmit')
              // 从后端返回信息
              this.$alert(resp.data.message, '提示', {
                type: 'success',
                dangerouslyUseHTMLString: true
              })
            } else {
              // 从后端返回错误信息
              this.$alert(resp.data.message, '错误提示', {
                type: 'error',
                dangerouslyUseHTMLString: true
              })
            }
        })
      }
    }
  }
</script>

<style scoped>
  .add-button {
     margin: 0px 0 5px 15px;
  }
</style>
